package org.eae.eldf.dao.base.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.apache.commons.lang.StringUtils;
import org.eae.eldf.dao.base.IBaseCompanyInfoDao;
import org.eae.eldf.dto.base.BaseCompanyInfoQueryDto;
import org.eae.eldf.entity.base.BaseCompanyInfo;
import org.eae.eldf.framework.dao.JdbcDaoImpl;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.utils.StringUtil;

@Repository
public class BaseCompanyInfoDaoImpl extends JdbcDaoImpl implements IBaseCompanyInfoDao{

	@Override
	public Pagination<BaseCompanyInfo> queryListBaseCompanyInfo(
			Pagination<BaseCompanyInfo> pager, BaseCompanyInfoQueryDto baseCompanyInfoQueryDto) {
		String sql="select c.*,JSON_UNQUOTE(c.company_name) as companyName,JSON_UNQUOTE(json_extract (c.company_name, '$.zh_CN')) AS companyNameCn,JSON_UNQUOTE(json_extract (c.company_name, '$.en_US'))AS companyNameEn from base_company_info c";
		List<Object> pars=new ArrayList<Object>();
		if(baseCompanyInfoQueryDto!=null&&baseCompanyInfoQueryDto.getCompanyCode()!=null&&!StringUtil.isNullOrBlank(baseCompanyInfoQueryDto.getCompanyCode())){
			sql+=" where c.company_code  like ?";
			pars.add("%"+baseCompanyInfoQueryDto.getCompanyCode()+"%");
		}
		if(StringUtils.isNotBlank(pager.getOrderBy()) && StringUtils.isNotBlank(pager.getSort())){
			sql+=" ORDER BY "+pager.getOrderBy().toUpperCase()+" "+pager.getSort().toUpperCase();
		}else{
			sql+=" ORDER BY company_code DESC";
		}
		return this.queryListByPage(pager, sql,pars,BaseCompanyInfo.class);
	}


	@Override
	public void updateCompany(BaseCompanyInfo c) {
		String sql = "UPDATE BASE_COMPANY_INFO SET COMPANY_NAME = '"+c.getCompanyName()+"',COMPANY_NAME_KEY = '"+c.getCompanyNameKey()+"',COMPANY_ADDR = '"+c.getCompanyAddr()+"',COMPANY_REMARK = '"+c.getCompanyRemark()+"',COMPANY_SCALE = '"+c.getCompanyScale()+"',IS_ENABLE = '"+c.getIsEnable()+"', exta='"+c.getExta()+"', extb='"+c.getExtb()+"',extc='"+c.getExtc()+"', extd='"+c.getExtd()+"', exte='"+c.getExte()+"' WHERE BASE_COMPANY_INFO_ID = '"+c.getBaseCompanyInfoId()+"'";
		this.executeBySQL(sql);
	}


	@Override
	public List<BaseCompanyInfo> queryCompanyList() {
		String sql="select c.*,JSON_UNQUOTE(c.company_name) as companyName,JSON_UNQUOTE(json_extract (c.company_name, '$.zh_CN')) AS companyNameCn,JSON_UNQUOTE(json_extract (c.company_name, '$.en_US'))AS companyNameEn from base_company_info c";
		
		return this.queryBySQL(sql, BaseCompanyInfo.class);
	}

}
